<template>
  <div class="product-attr-group">
    <el-form
      ref="EditForm"
      :model="editModel"
      label-width="120px"
      :rules="validate"
    >
      <el-row>
        <el-col :span="12">
          <el-form-item label="属性分组名称:" prop="name">
            <el-input
              v-model="editModel.name"
              placeholder="属性分组名称"
              maxlength="20"
              show-word-limit
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="商品分类:" prop="categoryId">
            <el-tree-select
              ref="treeSelect"
              v-model="editModel.categoryId"
              :styles="styles"
              :select-params="selectParams"
              :tree-params="treeParams"
              @searchFun="_searchFun"
            />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item label="分组排序:" prop="sort">
            <el-input-number
              v-model="editModel.sort"
              style="width: 100%"
              :step="1"
              :max="100"
              :min="0"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="分组描述:" prop="description">
            <el-input
              v-model="editModel.description"
              type="textarea"
              resize="none"
              rows="5"
              placeholder="描述"
              show-word-limit
              maxlength="255"
              clearable
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>
<script>
import { listToTree } from 'tree-walk-util'
import { findAllCategoryREQ } from '@/api/product/category'
export default {
  name: 'EditForm',
  components: {},
  props: {
    editModel: {
      type: Object
    },
    validate: {
      type: Object
    }
  },
  data() {
    return {
      styles: {
        width: '100%'
      },
      selectParams: {
        multiple: false,
        clearable: true,
        placeholder: '请输入内容'
      },
      treeParams: {
        clickParent: true,
        filterable: true,
        'highlight-current': true,
        'check-strictly': true,
        'default-expand-all': true,
        'expand-on-click-node': false,
        data: [],
        props: {
          children: 'children',
          label: 'text',
          disabled: 'status',
          value: 'value'
        }
      }
    }
  },
  computed: {

  },
  mounted() {
    findAllCategoryREQ().then((data) => {
      if (this.editModel.id && data && data.length > 0) {
        const temp = data.find(v => {
          return v.value === this.editModel.id
        })
        if (temp) temp.disabled = 'true'
      }
      const tempData = listToTree(data, 'value', 'parentId', 'children').children
      this.$refs.treeSelect.treeDataUpdateFun(tempData)
    })
  },
  methods: {
    _searchFun(value) {
      this.$refs.treeSelect.filterFun(value)
    }
  }

}
</script>
<style lang='scss' scoped>

</style>
